using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using System.Threading;
using System.Diagnostics;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class BaseForm : Form, ISupportDialogKeys
    {
        public event EventHandler<DialogKeyEventArgs> ProcessingDialogKey;

        public BaseForm()
        {
            InitializeComponent();
        }

        protected void HandleException(Exception ex)
        {
            UIManager.Instance.ExceptionForm.Show(ex);

        }

        [DebuggerHidden]
        public void InDisplay(Operation action)
        {
            if (this.IsHandleCreated)
            {
                this.Invoke(action);
            }
            else
            {
                this.HandleCreated += delegate
                {
                    this.Invoke(action);
                };
            }
        }

        [DebuggerHidden]
        protected Thread InBackground(Operation action)
        {
            return this.InBackground(action, null);
        }


        [DebuggerHidden]
        protected Thread InBackground(Operation action, Operation callback)
        {
            Thread thread = new Thread(new ThreadStart(delegate
            {
                try
                {
                    action();
                    if (callback != null)
                    {
                        callback();
                    }
                }
                catch (Exception ex)
                {
                    if (ex is ThreadAbortException || (ex.InnerException != null && ex.InnerException is ThreadAbortException))
                    {

                    }
                    else
                    {
                        this.HandleException(ex);
                    }
                }
            }));

            thread.Priority = ThreadPriority.Normal;
            thread.IsBackground = true;
            thread.Start();

            return thread;


        }


        protected string Inputbox(
            Icon icon,
            string title,
            string description,
            string prompt,
            bool hideText,
            Control customControl)
        {
           return UIUtils.Inputbox(this, icon, title, description, prompt, hideText,customControl);
        }

        protected  bool YesNo(
             Icon icon,
             string title,
             string description)
        {
            return UIUtils.YesNo(this, icon, title, description);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (ProcessingDialogKey != null)
            {
                DialogKeyEventArgs args = new DialogKeyEventArgs(keyData);
                ProcessingDialogKey(this, args);
            }
            return base.ProcessDialogKey(keyData);
        }

        protected void SetupDialogWork(Button cancelButton, Button okButton, Fn<bool> work)
        {

            bool ignore = false;
            this.FormClosing += delegate(object sender, FormClosingEventArgs fe)
            {

                if (ignore || this.DialogResult != DialogResult.OK)
                {
                    return;
                }
                fe.Cancel = true;
                this.Cursor = Cursors.WaitCursor;

                cancelButton.Focus();
                okButton.Text = "Saving...";
                okButton.Enabled = false;

                Exception ex = null;
                bool? rt = null;
                Thread t = this.InBackground(delegate
                {
                    try
                    {
                        rt = work();
                    }
                    catch (Exception e)
                    {
                        ex = e;
                    }

                }, delegate
                {
                    this.InDisplay(delegate
                    {
                        this.Cursor = Cursors.Default;
                        if (ex == null && rt.HasValue && rt.Value)
                        {
                            this.DialogResult = DialogResult.OK;
                            ignore = true;
                            this.Close();
                        }
                        else
                        {
                            if (ex != null)
                            {
                                this.HandleException(ex);
                            }
                            okButton.Enabled = true;
                            okButton.Text = "Save";
                        };

                    });

                });

            };
        }

    }
}